import { _decorator, Component, instantiate, Prefab } from 'cc';
const { ccclass, property } = _decorator;
import { Utils } from './Utils';
import { GameManager, GameStatus } from './GameManager';

@ccclass('SpawnPipe')
export class SpawnPipe extends Component {
  @property({ tooltip: '生成速率' })
  SpawnSpeed: number = 1
  @property({ tooltip: '计时器' })
  Timer: number = 0
  @property({ type: Prefab })
  PipePrefab: Prefab
  start() {

  }

  update(deltaTime: number) {
    if (GameManager.status === GameStatus.GamePlay) {
      if (this.Timer >= 2) {
        this.Timer = 0
        const PipePrefabInstance = instantiate(this.PipePrefab)
        PipePrefabInstance.setPosition(0, Utils.getRandomIntInclusive(-290, 290))
        this.node.addChild(PipePrefabInstance)
      }
      else {
        this.Timer = this.Timer + this.SpawnSpeed * deltaTime
      }
    }
  }
}